package com.smallrig.mall.template.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.smallrig.mall.template.entity.Common;
import com.smallrig.mall.template.entity.OrderInfo;
import com.smallrig.mall.template.mapper.OrderMapper;
import com.smallrig.mall.template.service.ICommonService;
import com.smallrig.mall.template.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.spring.transaction.TransactionTypeInterceptor;
import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lry
 * @since 2022-08-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderInfo> implements IOrderService {

    @Resource
    private ICommonService commonService;


//    TransactionTypeInterceptor
//com.atomikos.icatch.jta.JtaTransactionServicePlugin.beforeInit
//    ShardingSphereConnection
    //org.apache.shardingsphere.driver.jdbc.core.connection.ConnectionManager
    @Override
    @Transactional
//    @ShardingSphereTransactionType(TransactionType.XA)
    public void createOrder() {
//        TransactionTypeHolder.set(TransactionType.LOCAL);
        OrderInfo order = new OrderInfo();
        long userId = 1;
        order.setUserId(userId);
        order.setCreateTime(LocalDateTime.now());
        order.setOrderNo(generateOrderNo(userId));
        save(order);

        userId = 2;
        order.setUserId(userId);
        order.setCreateTime(LocalDateTime.now().plusYears(1));
        order.setOrderNo(generateOrderNo(userId));
        save(order);

        Common common = new Common();
        common.setCreateTime(LocalDateTime.now());
        commonService.save(common);

        int i = 1/0;
    }

    @Override
    public List<OrderInfo> queryByUserId(Long userId){
        return lambdaQuery().eq(OrderInfo::getUserId,userId).list();
    }

    @Override
    public List<OrderInfo> queryByOrderNo(String orderNo){
        return lambdaQuery().eq(OrderInfo::getOrderNo,orderNo).list();
    }

    private String generateOrderNo(Long userId) {
        String uid = userId.toString();
        if (uid.length() > 4) {
            uid = uid.substring(uid.length() - 4);
        } else {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < 4 - uid.length(); i++) {
                str.append("0");
            }
            str.append(uid);
            uid = str.toString();
        }
        return IdWorker.getId() + "" + uid;
    }
}
